package com.sumainfor.pc.market.controller;

import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ListMapMergeUtil;
import com.sumainfor.common.utlis.PageUtils;
import com.sumainfor.common.utlis.Pager;
import com.sumainfor.pc.market.feign.BaseFeignZuul;
import com.sumainfor.pc.market.service.CommunityService;
import com.sumainfor.pc.market.util.Distance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Auther: zhlu
 * @Date: 2018/11/3 21:15
 * @Description:
 */
@RestController
@RequestMapping("/com")
public class CommunityController implements Serializable{

    @Autowired
    CommunityService communityService;

    @Autowired
    BaseFeignZuul BaseFeignZuul;

    /**
     * 功能描述:
     * 新增项目（小区）
     * @Author zhlu
     * @Description //TODO
     * @Date 2018/11/3 21:22
     */
    @RequestMapping(value = "/AddCommunity",method = RequestMethod.POST)
    public JsonResult AddCommunity(@RequestBody Map<String,Object>params){
        JsonResult result=new JsonResult();
        if(communityService.AddCommunity(params)){
            result.putSuccess("新增项目成功！");
        }else{
            result.putFailed("服务器繁忙,请稍后重试！");
        }
        return result;
    }

    /**
     * 获取地理位置
     * @Description: TODO(这里用一句话描述这个方法的作用)
     * @author zhlu
     * @date 2018年4月11日
     */
    @ResponseBody
    @RequestMapping(value="getDistance",method=RequestMethod.GET)
    public JsonResult getDistance(@RequestParam Map<String,Object>params){
        JsonResult result=new JsonResult();
        Map<String,Object>dist= Distance.getLngLatFromOneAddr(params);
        if(dist==null || dist.size()<1){
            result.putFailed("获取地理位置失败，请重新输入地址！");
        }else{
            result.put(dist);
        }
        return result;
    }
    /**
     * 功能描述:
     * 根据小区名称和路号判断是否该小区重名
     * @Author zhlu
     * @Description //TODO
     * @Date 2018/11/7 15:16
     */
    @ResponseBody
    @GetMapping("/IfCommunity")
    public JsonResult IfCommunity(@RequestParam Map<String,Object>params){
        JsonResult result=new JsonResult();
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"ComName"})){
            result.putFailed("ComName不能为空！");
            return  result;
        }
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"Road"})){
            result.putFailed("Road不能为空！");
            return  result;
        }
        if(communityService.IfCommunity(params)<=0){
            result.putSuccess();
        }else{
            result.putFailed("已经存在该小区！");
        }
        return  result;
    }

    /**
     * 功能描述:
     * 查询项目列表
     * @Author zhlu
     * @Description //TODO
     * @Date 2018/11/8 14:44
     */
    @ResponseBody
    @GetMapping("/getCommunityList")
    public JsonResult getCommunityList(@RequestParam Map<String,Object>params,Pager pager){
        JsonResult result=new JsonResult();
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();
        Integer cout=communityService.getCommunityListCount(params);
        if(cout==0){
            return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(), params,cout);
        }
        List<Map<String,Object>>getCommunityList=communityService.getCommunityList(params);
        result=pageUtils.getJsonResult(getCommunityList, params, cout);
        return result;
    }

    /**
     * 功能描述:
     * 根据项目编号获取项目
     * @Author zhlu
     * @Description //TODO
     * @Date 2018/11/8 18:57
     */
    @ResponseBody
    @GetMapping("/getCommunity")
    public JsonResult getCommunity(@RequestParam Map<String,Object>params){
        JsonResult result=new JsonResult();
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"ComId"})){
            result.putFailed("ComId不能为空！");
            return  result;
        }
        Map<String,Object>getCommunity=communityService.getCommunity(params);
        List<Map<String,Object>>getDeptList=BaseFeignZuul.getDeptList(params);
        if(FieldCheckUtil.isMapFieldNotNull(getCommunity,new String[]{"MiId"})){
            getCommunity= ListMapMergeUtil.MapTranStringUtils(getCommunity,getDeptList,"MiId","DeptId","DeptName","MiName");
        }
        return result.put(getCommunity);
    }

    /**
     * 功能描述:
     * 根据项目编号修改项目设备
     * @Author zhlu
     * @Description //TODO
     * @Date 2018/11/9 11:58
     */
    @ResponseBody
    @RequestMapping(value = "/updateCommunity",method = RequestMethod.POST)
    public JsonResult updateCommunity(@RequestBody Map<String,Object>params){
        JsonResult result=new JsonResult();
        if(communityService.updateCommunity(params)){
            result.putSuccess("修改项目成功！");
        }else{
            result.putFailed("服务器繁忙，请稍后重试！");
        }
        return result;
    }

    /**
     * 功能描述:
     * 根据省市区街道获取项目编号列表
     * @Author zhlu
     * @Description //TODO
     * @Date 2018/11/11 11:29
     */
    @ResponseBody
    @GetMapping("/SelectCommunityList")
    public JsonResult SelectCommunityList(@RequestParam Map<String,Object>params){
        params.put("MiId",params.get("DeptId"));
        JsonResult result=new JsonResult();
        List<Map<String,Object>>SelectCommunityList=communityService.SelectCommunityList(params);
        List<Map<String,Object>>getDeptList=BaseFeignZuul.getDeptList(params);
        for(Map<String,Object> map:SelectCommunityList){
            if(FieldCheckUtil.isMapFieldNotNull(map,new String[]{"MiId"})){
                map=ListMapMergeUtil.MapTranStringUtils(map,getDeptList,"MiId","DeptId","DeptName","MiName");
            }
        }
//        SelectCommunityList= ListMapMergeUtil.ListTranStringUtils(SelectCommunityList,getDeptList,"MiId","DeptId","DeptName","MiName");
        if(SelectCommunityList.size()<=0){
            result.putFailed("暂无该地区项目！");
        }else{
            result.put(SelectCommunityList);
        }
        return result;
    }

    /**
     * 功能描述: 
     * 获取项目总数
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/4/13 16:04
     */
    @GetMapping("/getCommunityCount")
    public JsonResult getCommunityCount(){
        JsonResult result=new JsonResult();
        return result.put(communityService.getCommunityCount());
    }

    /**
     * 功能描述:
     * 根据服务中心编号获取所有项目列表
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/20 18:57
     */
    @PostMapping("/getDepIdByCommunityList")
    public JsonResult getDepIdByCommunityList(@RequestBody Map<String,Object>params){
        if(FieldCheckUtil.isMapFieldNotNull(params,new String[]{"deptList"})){
            List<String>deptList=(List<String>)params.get("deptList");
            if(deptList.size()==0){
                return JsonResult.error("deptList不能为空！");
            }
        }
        return JsonResult.success(communityService.getDepIdByCommunityList(params));
    }
}
